<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

      <el-form-item label="患者姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入患者姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardNum">
        <el-input v-model="queryParams.idcardNum" placeholder="请输入身份证号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <!-- 序号、患者姓名、患者头像、身份证号、病历编号、病历详情、服务部门、服务医生、创建时间 -->
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="userName" />
      <el-table-column label="患者头像" width="100" align="center" prop="userAvatar">
        <template #default="scope">
          <img width="50" height="50" :src="scope.row.userAvatar" alt="">
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idcardNum" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="服务记录" align="center" prop="medicalRecordDetail">
        <template #default="scope">
          <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">服务记录</el-button>
        </template>
      </el-table-column>


    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改公告对话框 -->
    <el-dialog title="服务记录" v-model="open" width="1080px" append-to-body>
      <template #header>
        <div>
          <!--  -->
          <Header :key="form.appUserId" title="服务记录" :queryParams="form" />
          <el-form :inline="true" :model="queryParams" ref="queryRef" v-show="showSearch">
            <el-form-item label="服务日期" prop="date">
              <el-date-picker v-model="queryParams.date" style="width: 150px" format="YYYY/MM/DD"
                value-format="YYYY-MM-DD" type="date" placeholder="请选择日期" />
            </el-form-item>
            <el-form-item label="服务部门" prop="departmentName">
              <el-input v-model="queryParams.departmentName" placeholder="请输入服务部门" clearable style="width: 150px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="服务医生" prop="employeeName">
              <el-input v-model="queryParams.employeeName" placeholder="请输入服务医生" clearable style="width: 150px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="服务类型" prop="type">
              <el-select v-model="queryParams.type" placeholder="请选择服务类型" style="width: 150px">
                <el-option v-for="item, index in [
                  {
                    label: '坐诊看病',
                    value: '1'
                  },
                  {
                    label: '治疗护理',
                    value: '2'
                  },
                  {
                    label: '共享护工',
                    value: '3'
                  },
                  {
                    label: '家庭医生',
                    value: '4'
                  },
                  {
                    label: '紧急呼救',
                    value: '5'
                  },
                  {
                    label: '共享护士',
                    value: '9'
                  }
                ]" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>


            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <div class="flex">
        <div style=" flex-grow: 1;">
          <el-table height="450px" scrollbar-always-on highlight-current-row size="small" :data="tableData"
            @current-change="handleCurrentChange">
            <el-table-column label="序号" align="center" type="index" width="55">
              <template #default="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="type" label="类型">
              <template #default="scope">
                <dict-tag :options="[
                  {
                    label: '坐诊看病',
                    value: '1'
                  },
                  {
                    label: '治疗护理',
                    value: '2'
                  },
                  {
                    label: '共享护工',
                    value: '3'
                  },
                  {
                    label: '家庭医生',
                    value: '4'
                  },
                  {
                    label: '紧急呼救',
                    value: '5'
                  },
                  {
                    label: '共享护士',
                    value: '9'
                  }
                ]" :value="(scope.row.type)" />
              </template>
            </el-table-column>
            <el-table-column prop="date" label="服务时间" width="100" />
            <el-table-column prop="departmentName" label="服务部门" width="100" />
            <el-table-column prop="name" label="服务医生" width="100" />
          </el-table>
        </div>
        <div class="detail" style="flex: 1 1 50%;">

          <div class="message-list">
            <!-- :class="[item.isOwner ? 'flex-end' : 'flex-start']" -->
            <div class="message-item" v-for="item in groupList" :key="item.id">
              <template v-if="item.messageType == 1">
                <img class="avatar" :src="item.avatar">
                <div class="message-content">
                  <span class="name">{{ item.name }}</span>
                  <p class="content">{{ item.content }}</p>
                </div>
              </template>
              <template v-else-if="item.messageType == 2">
                <img class="avatar" :src="item.avatar">
                <div class="message-content">
                  <span class="name">{{ item.name }}</span>
                  <!-- <p class="content">图片消息</p> -->
                  <img class="" :src="item.content">
                  <!-- <ImageUpload v-model="item.content" /> -->
                </div>
              </template>
              <template v-else-if="item.messageType == 3">
                <img class="avatar" :src="item.avatar">
                <div class="message-content">
                  <span class="name">{{ item.name }}</span>
                  <p class="content flex" style="align-items: center;">
                    <!-- <el-icon>
                      <Document />
                    </el-icon> -->
                    <img class="avatarss" src="/src/assets/logo/fileUpload.png" alt="">
                  <div style="margin-left: 5px;"> 视频消息</div>
                  </p>
                </div>
              </template>
              <template v-else-if="item.messageType == 4">
                <img class="avatar" :src="item.avatar">
                <div class="message-content">
                  <span class="name">{{ item.name }}</span>
                  <p class="content flex" style="align-items: center;">
                    <!-- <el-icon>
                      <Document />
                    </el-icon> -->
                    <img class="avatarss" src="/src/assets/logo/fileUpload.png" alt="">
                  <div style="margin-left: 5px;"> 语音消息</div>
                  </p>
                </div>
              </template>
              <template v-else>
                <img class="avatar" :src="item.avatar">
                <!-- <el-icon :size="32" ><House /></el-icon> -->
                <div class="message-content">
                  <span class="name">{{ item.name }}</span>
                  <p class="content flex" style="align-items: center;">
                    <!-- <el-icon>
                      <Document />
                    </el-icon> -->
                    <img class="avatarss" src="/src/assets/logo/fileUpload.png" alt="">
                  <div style="margin-left: 5px;"> {{ item.cardName }}</div>
                  </p>
                </div>
              </template>

            </div>

            <!-- 更多的消息项 -->
          </div>
        </div>
      </div>
    </el-dialog>


  </div>
</template>

<script setup name="Notice">
const tableData = ref([])
const groupList = ref([])

import Header from './common/header.vue'

import {
  servicemessagelist,
  groupServApiservicelist,
  medicalApiRecordDetail,
  userlist,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/backGroupServ/backGroupServ";


// import {
//   medicalApiRecordDetail,
//   medicalApiRecordDetailList,
//   listRecord,
//   getNotice,
//   delNotice,
//   addNotice,
//   updateNotice,
// } from "@/api/backMedical/backMedical";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type, sys_user_sex } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type",
  "sys_user_sex"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {
  },
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    userName: undefined,
    idcardNum: undefined,
    medicalRecordId: undefined,
    date: undefined,
    departmentName: undefined,
    employeeName: undefined,
    appUserId: undefined,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);


// 禁用所有表单
const formDisable = ref(false)


let showForm = ref(false);
/**详情按钮操作 */
function handleDetail(row) {
  reset();
  console.log(row, 'row');

  // 获取appUserId
  const appUserId = row.appUserId || ids.value;
  queryParams.value.appUserId = appUserId

  // form.value = { ...form.value, ...row }
  form.value = { ...row }


  groupServApiservicelist(queryParams.value).then((response) => {
    console.log(response, 'response');
    tableData.value = response;
    open.value = true;
    title.value = "详情病历";
  }).catch((error) => {
    console.log(error, 'error');
  });
  formDisable.value = true
}

/** 查询公告列表 */
function getList() {
  loading.value = true;
  userlist(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    "allergyHistory": "",
    "createTime": "",
    "departmentName": "",
    "diagnose": "",
    "employeeName": "",
    "medicalHistory": "",
    "medicalRecordId": "",
    "medicalState": "",
    "merchantName": "",
    "userAge": "",
    "userGender": "",
    "userHeight": "",
    "userIsMarry": "",
    "userName": "",
    "userPhone": "",
    "userWeight": ""
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();


  // 获取appUserId
  groupServApiservicelist(queryParams.value).then((response) => {
    tableData.value = response;
    open.value = true;
    title.value = "详情病历";
  });

}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

function handleCurrentChange(row) {
  if (row?.groupId) {
    servicemessagelist({
      currentPage: 1,
      pageSize: 9999,
      groupId: row.groupId
    }).then((response) => {
      console.log('respons311e', response);
      // groupList.value = response.records.filter(item => item.messageType == 1)
      groupList.value = response.records

      form.value = response
    })
  }
}

getList();
</script>

<style lang="scss" scoped>

.avatarss{
  width: 20px;
  height: 20px;
}
.detail {
  :deep(.el-input__wrapper.is-focus) {
    box-shadow: none !important;
  }

  :deep(.el-input__wrapper:hover) {
    box-shadow: none !important;
  }

  :deep(.el-input__wrapper) {
    box-shadow: none !important;
  }

  :deep(.el-input.is-disabled .el-input__wrapper) {
    background-color: #ffff !important;
  }

  :deep(.el-input.is-disabled .el-input__inner) {
    color: #000 !important;
    -webkit-text-fill-color: #000 !important;
  }

  :deep(.el-textarea__inner) {
    background-color: #ffff !important;
    border: none !important;
    box-shadow: none !important;
    color: #000 !important;
    -webkit-text-fill-color: #000 !important;
  }
}

/* 聊天 */
.flex-end {
  justify-content: flex-end;
}

.flex-start {
  justify-content: flex-start;
}

/* 消息列表 */
.message-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  max-width: 500px;
  margin: 0 auto;
  height: 450px;
  overflow: hidden;
  overflow-y: auto;
  background-color: #f9f9f9;
}

/* 每一条消息 */
.message-item {
  // 
  display: flex;
  gap: 15px;
  align-items: flex-start;
  padding: 10px;
  background-color: #F8F8F9;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 头像样式 */
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
}

/* 消息内容 */
.message-content {
  display: flex;
  width: 150px;
  flex-direction: column;
  gap: 5px;
}

/* 姓名样式 */
.name {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* 消息内容样式 */
.content {
  font-size: 14px;
  color: #666;
  margin: 0;
}
</style>
